using System;
using System.Collections.Generic;
using System.Text;
using QLNT_DTO;
using System.Data;
using System.Data.SqlClient;


namespace QLNT_DAO
{
    public class DonGiaThuoc_dao
    {
        public int ThietLapDonViTinh(int MaThuoc, int MaDangThuoc, int MaXuatXu, int MaDonViTinh, float DonGiaBan)
        {
            int n = 0;
            try
            {
                //tao list parameter {cac parameter chinh la cac tham so dau vao cua procedure}
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@MaThuoc", MaThuoc));
                listpara.Add(new SqlParameter("@MaDangThuoc", MaDangThuoc));
                listpara.Add(new SqlParameter("@MaXuatXu", MaXuatXu));
                listpara.Add(new SqlParameter("@MaDonViTinh", MaDonViTinh));
                listpara.Add(new SqlParameter("@DonGiaBan", DonGiaBan));
                n = MyExecute.ExecuteNoneQuery("sp_ThietLapDonGiaBan", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return n;
        }

        public int ThayDoiDonGiaThuoc(int MaThuoc, int MaDangThuoc, int MaXuatXu, int MaDonViTinh, float DonGiaBan)
        {
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@MaThuoc", MaThuoc));
                listpara.Add(new SqlParameter("@MaDangThuoc", MaDangThuoc));
                listpara.Add(new SqlParameter("@MaXuatXu", MaXuatXu));
                listpara.Add(new SqlParameter("@MaDonViTinh", MaDonViTinh));
                listpara.Add(new SqlParameter("@DonGiaBan", DonGiaBan));
                return MyExecute.ExecuteNoneQuery("sp_CapNhatDonGiaBan", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int XoaDonGiaThuoc(int MaThuoc, int MaDangThuoc, int MaXuatXu, int MaDonViTinh)
        {
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@MaThuoc", MaThuoc));
                listpara.Add(new SqlParameter("@MaDangThuoc", MaDangThuoc));
                listpara.Add(new SqlParameter("@MaXuatXu", MaXuatXu));
                listpara.Add(new SqlParameter("@MaDonViTinh", MaDonViTinh));
                return MyExecute.ExecuteNoneQuery("sp_XoaDonGiaBan", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DonGiaThuoc_dto[] SelectAll()
        {
            DonGiaThuoc_dto[] dg = new DonGiaThuoc_dto[100];
            DataTable dt = new DataTable();
            try
            {
                dt = MyExecute.ExecuteQuery("sp_LayDSDonGiaThuoc");
                int n = dt.Rows.Count;
                dg = new DonGiaThuoc_dto[n];
                for (int i = 0; i < n; i++)
                {
                    dg[i] = new DonGiaThuoc_dto();
                    dg[i].MaThuoc = int.Parse(dt.Rows[i]["MaThuoc"].ToString());
                    dg[i].MaDangThuoc = int.Parse(dt.Rows[i]["MaDangThuoc"].ToString());
                    dg[i].MaXuatXu = int.Parse(dt.Rows[i]["MaXuatXu"].ToString());
                    dg[i].MaDonViTinh = int.Parse(dt.Rows[i]["MaDonViTinh"].ToString());
                    dg[i].DonGia = float.Parse(dt.Rows[i]["DonGiaBan"].ToString());

                    dg[i].TenThuoc = dt.Rows[i]["TenThuoc"].ToString();
                    dg[i].TenDangThuoc = dt.Rows[i]["TenDangThuoc"].ToString();
                    dg[i].TenXuatXu = dt.Rows[i]["TenXuatXu"].ToString();
                    dg[i].TenDonViTinh = dt.Rows[i]["TenDonViTinh"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dg;
        }

        public DonGiaThuoc_dto GetDonGiaThuoc(int MaThuoc, int MaDangThuoc, int MaXuatXu, int MaDonViTinh)
        {
            List<SqlParameter> listpara = new List<SqlParameter>();
            listpara.Add(new SqlParameter("@MaThuoc", MaThuoc));
            listpara.Add(new SqlParameter("@MaDangThuoc", MaDangThuoc));
            listpara.Add(new SqlParameter("@MaXuatXu", MaXuatXu));
            listpara.Add(new SqlParameter("@MaDonViTinh", MaDonViTinh));

            DataTable dt = MyExecute.ExecuteQuery("sp_LayDonGiaThuocTuongUng", listpara);
            if (dt.Rows.Count >= 1)
            {
                DonGiaThuoc_dto dg = new DonGiaThuoc_dto();
                dg.MaThuoc = int.Parse(dt.Rows[0]["MaThuoc"].ToString());
                dg.MaDangThuoc = int.Parse(dt.Rows[0]["MaDangThuoc"].ToString());
                dg.MaXuatXu = int.Parse(dt.Rows[0]["MaXuatXu"].ToString());
                dg.MaDonViTinh = int.Parse(dt.Rows[0]["MaDonViTinh"].ToString());
                dg.DonGia = float.Parse(dt.Rows[0]["DonGiaBan"].ToString());

                dg.TenThuoc = dt.Rows[0]["TenThuoc"].ToString();
                dg.TenDangThuoc = dt.Rows[0]["TenDangThuoc"].ToString();
                dg.TenXuatXu = dt.Rows[0]["TenXuatXu"].ToString();
                dg.TenDonViTinh = dt.Rows[0]["TenDonViTinh"].ToString();
                return dg;
            }
            return null;
        }
    }
}
